package service.Imp;


import Message.DTO.SetmealDTO;
import Message.DTO.SetmealPageQueryDTO;
import Message.PageResult;
import Message.entity.Setmeal;
import Message.entity.SetmealDish;
import Message.vo.DishItemVO;
import Message.vo.SetmealVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import constant.StatusConstant;
import lombok.extern.slf4j.Slf4j;
import mapper.SetmealDishMapper;
import mapper.SetmealMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.SetmealService;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImp implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public PageResult SelectPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 1.设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        // 2.调用mapper的列表查询方法，强转成Page
        Page<SetmealVO> page = setmealMapper.SelectPage(setmealPageQueryDTO);
        log.info("page:{}", page);

        // 3.封装PageResult对象并返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     *
     * 根据id 查套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO SelectById(Long id) {

        SetmealVO setmealVO = new SetmealVO();


        Setmeal setmeal = setmealMapper.SelectByid(id);
        BeanUtils.copyProperties(setmeal, setmealVO);

        List<SetmealDish> setmealDish = setmealDishMapper.SelectByid(id);
        setmealVO.setSetmealDishes(setmealDish);
        log.info("setmealVO:{}", setmealVO);

        return setmealVO;

    }

    @Transactional
    @Override
    public void Delete(List<Long> ids) {

        //if 判断ids 如果为空则不执行
        if (ids != null || ids.size() != 0 ) {
        //调用setmealMapper 删除setmeal 数据

            setmealMapper.Delete(ids);

        //调用 setmealDishMapper 删除setmealDishes 中对应id 的数据

         setmealDishMapper.Delete(ids);
        }




    }

    @Override
    @Transactional//两张表操作添加事务
    public void AddSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //设置状态 1表示起售 2表示停售
        setmeal.setStatus(StatusConstant.ENABLE);

        //新增套擦
        setmealMapper.insert(setmeal);

        //获取套餐生成id
        Long id = setmeal.getId();

        List<SetmealDish> setmealDishes =setmealDTO.getSetmealDishes();

        if(setmealDishes!=null&&setmealDishes.size()!=0){


            setmealDishes.forEach(setmealDish->{

                setmealDish.setSetmealId(id);


            });
            //批量插入套餐对应的套餐菜品数据
            setmealDishMapper.insert(setmealDishes);

        }

    }

    @Override
    public List<SetmealVO> list(Long categoryId) {
        //调Mapping层查询数据库信息返回参数
        List<SetmealVO> setmealVO = setmealMapper.List(categoryId);

        return setmealVO;
    }

    @Override
    public List<DishItemVO> ListDishById(Long dishId) {


    List<DishItemVO> dishItemVOS = setmealDishMapper.ListDishById(dishId);

return dishItemVOS;
    }

    @Override
        public void UpdateSetmeal(SetmealDTO setmealDTO) {

        log.debug("setmealDTO:{}", setmealDTO);

        // 涉及两张表操作，首先new 第一张表修改里面的数据
        Setmeal setmeal = new Setmeal();
        //把拿到的数据复制到new出的新表
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        Long id = setmealDTO.getId();
        List ids = new ArrayList();ids.add(id);
        setmealDishMapper.Delete(ids);

        List<SetmealDish> dishes =setmealDTO.getSetmealDishes();
        if(dishes!=null&&dishes.size()!=0){
            dishes.forEach(dish->{

                dish.setSetmealId(setmealDTO.getId());
            });

            setmealDishMapper.insert(dishes);


        }





    }
}
